/*
* Copyright (c) 2024 SwanLink (Jiangsu) Technology Development Co., LTD.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_yfree/YFree.dart';

import 'MyAlert.dart';
import 'MyBroadcast.dart';
import 'MyPluginPage.dart';
import 'MyUtils.dart';
import 'MyWidget.dart';
import 'MyWidget2.dart';

class MyPage extends YPage {
  var title = "";

  @override
  void yOnCreate() {
    setState(() => title = yGetAppName());
    yShowBuilder("本项目仅可用于学习使用，非作者本人同意，请勿传播或用做任何商业用途。\n感谢您的支持！",
        () => showToast(), null, false);
    yBroadcast_register("broadcastKey", (params) => yToast(params!["msg"]));
    YConfig.catchCrash = false;
  }

  Future<void> showToast() async {
    yBroadcast_send("broadcastKey", params: {"msg": "欢迎使用YFree_Flutter"});
  }

  Widget view_button(String title, Function() onTap) {
    return YButton(
      child: Text(title,
          style: const TextStyle(
              color: Colors.white, fontSize: 20, fontWeight: FontWeight.bold)),
      padding: const EdgeInsets.fromLTRB(10, 5, 10, 5),
      color: YConfig.themeColor,
      onTap: onTap,
    );
  }

  @override
  Widget yBuild(BuildContext context) {
    return Container(
      color: Colors.white,
      child: Column(
        children: <Widget>[
          YAppBar(title: title, canBack: false),
          Container(
            width: yScreenSize().width,
            padding: const EdgeInsets.all(5),
            child: Wrap(
              spacing: 5,
              runSpacing: 5,
              direction: Axis.vertical,
              children: [
                YButton(
                  decoration:
                      BoxDecoration(borderRadius: BorderRadius.circular(5)),
                  child: Text("Alert , Localization"),
                  enable: true,
                  onTap: () {
                    yIntent(MyAlert());
                  },
                ),
                YButton(
                  decoration:
                      BoxDecoration(borderRadius: BorderRadius.circular(5)),
                  child: Text("Broadcast , yPost"),
                  enable: true,
                  onTap: () {
                    yIntent(MyBroadcast());
                  },
                ),
                YButton(
                  decoration:
                      BoxDecoration(borderRadius: BorderRadius.circular(5)),
                  child: Text("Plugin        "),
                  enable: true,
                  onTap: () {
                    yIntent(MyPluginPage());
                  },
                ),
                YButton(
                  decoration:
                      BoxDecoration(borderRadius: BorderRadius.circular(5)),
                  child: Text("MyUtils , View"),
                  enable: true,
                  onTap: () {
                    yIntent(MyUtils());
                  },
                ),
                YButton(
                  decoration:
                      BoxDecoration(borderRadius: BorderRadius.circular(5)),
                  child: Text("输入法关闭的两个接口"),
                  enable: true,
                  onTap: () {
                    yIntent(MyWidget());
                  },
                ),
                YButton(
                  decoration:
                      BoxDecoration(borderRadius: BorderRadius.circular(5)),
                  child: Text("Widget UI组件"),
                  enable: true,
                  onTap: () {
                    yIntent(MyWidget2());
                  },
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }
}
